package br.com.games.daedal.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

import br.com.games.daedal.Box;
import br.com.games.daedal.DaedalTeam;
import br.com.games.daedal.Vertex;
import br.com.games.daedal.ia.Ares;
import br.com.games.daedal.imp.DaedalBoard;
import br.com.games.hefesto.Game;
import br.com.games.hefesto.Hefesto;
import br.com.games.hefesto.Move;
import br.com.games.hefesto.MoveListener;
import br.com.games.hefesto.Player;
import br.com.games.hefesto.board.Board;

public class DaedalUI extends JFrame implements ActionListener,
		MoveListener<Vertex> {
	/**
	 * 
	 * @param game
	 */
	public DaedalUI() {
		game = new SwingDaedal(new DaedalBoard(4,5), new SwingPlayer(this,
				DaedalTeam.WHITE), new Ares(DaedalTeam.BLACK, 7));
		this.board = new SwingBoard(game);

		setTitle("Challenge Dots");
		game.setBoard(this.board);

		setLayout(new BorderLayout());

		JToolBar toolBar = new JToolBar("Opções de jogo");

		JButton tmp;
		toolBar.add(new JButton("New"));
		toolBar.add(tmp = new JButton("Tip"));
		tmp.addActionListener(this);
		toolBar.add(new JButton("Und"));
		toolBar.addSeparator();
		toolBar.add(new JButton("Hlp"));

		add(toolBar, BorderLayout.PAGE_START);
		add(this.board, BorderLayout.CENTER);

		Hefesto<Board<Box>, Vertex> hf = new Hefesto<Board<Box>, Vertex>(game);
		hf.addMoveListener(this);
		new Thread(hf).start();
	}

	/**
	 * 
	 * @param b
	 */
	public void waitMove(boolean b) {
		this.board.waitMove(b);
	}

	/**
	 * 
	 * @return
	 */
	public Move<Vertex> getMove() {
		Move<Vertex> result = this.board.getMove();
		while (null == result) {
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result = this.board.getMove();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand().equals("Tip")) {
			Ares tmp = new Ares(DaedalTeam.WHITE, 3);

			@SuppressWarnings("unchecked")
			Move<Vertex> v = tmp
					.getMove((Game<? extends DaedalBoard, Vertex>) game);

			this.board.select(v.getMoveable(), Color.YELLOW);
		}else if (event.getActionCommand().equals("Und")) {
			 
			
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.hefesto.MoveListener#onMove(br.com.games.hefesto.Move,
	 * br.com.games.hefesto.Player)
	 */
	@Override
	public void onMove(Move<Vertex> move, Player<?, Vertex> palyer) {
		this.board.select(move.getMoveable(), Color.LIGHT_GRAY);
	}

	/**
	 * Campos privados
	 */
	private SwingBoard board;
	private SwingDaedal game;
	private static final long serialVersionUID = -4761641991033907614L;
}
